package rs.etf.es.algorithm;

public class AlgorithmBlocker {

    private static AlgorithmBlocker algorithmBlocker;
    private int selectedOperation;

    private AlgorithmBlocker() {
	setStopEnabled(true);
    }

    public static AlgorithmBlocker instance() {
	if (algorithmBlocker == null) {
	    algorithmBlocker = new AlgorithmBlocker();
	}

	return algorithmBlocker;
    }

    private boolean stopEnabled;

    public boolean isStopEnabled() {
	return stopEnabled;
    }

    public void setStopEnabled(boolean stopEnabled) {
	this.stopEnabled = stopEnabled;
    }

    public synchronized void stopHere() {
	if (stopEnabled) {
	    try {
		wait();
	    } catch (InterruptedException e) {
		stopEnabled = false;
	    }
	}
    }

    public synchronized void continueExecution() {
	selectedOperation = Constants.GO_NEXT;
	notifyAll();
    }

    public synchronized void backExecution() {
	selectedOperation = Constants.GO_PREVIOUS;
	notifyAll();
    }

    public synchronized void restartExecution() {
	selectedOperation = Constants.GO_RESTART;
	notifyAll();
    }

    public synchronized void finishExecution() {
	stopEnabled = false;
	notifyAll();
    }

    public int getSelectedOperation() {
	return selectedOperation;
    }

    public void setSelectedOperation(int selectedOperation) {
	this.selectedOperation = selectedOperation;
    }

}
